package com.bw.clock.view

import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.Log
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.RoutePath
import com.bw.clock.R
import com.bw.clock.databinding.ActivityAddClockBinding
import com.bw.clock.databinding.ActivityAddClockBindingImpl
import com.bw.clock.entity.KaoqinData
import com.bw.clock.net.ClockViewModel
import com.bw.common.view.BaseMvvmActivity
import java.text.SimpleDateFormat
import java.util.*

@Route(path = RoutePath.PATH_ADD_ClOCK)
class AddClockActivity : BaseMvvmActivity<ActivityAddClockBinding,ClockViewModel>() {
    val handeler = Handler(Looper.getMainLooper())
    var moring  : String = "2025/5/05 09:05:00"
    var afternoonCheckIn : String = "2025/5/05 16:00:00"
    val Uptime = object : Runnable {
        override fun run() {
            val millis = System.currentTimeMillis()
            val dateFormat = SimpleDateFormat("HH:mm:ss")
            val format: String = dateFormat.format(millis)
            binding.tvTime.text = format
            val dateFormat1 = SimpleDateFormat("yyyy年MM月dd日")
            val nowDate = dateFormat1.format(millis)
            val week = SimpleDateFormat("EEEE")
            val weekDate = week.format(millis)
            binding.tvCurrentDate.text = "$nowDate $weekDate"
            handeler.postDelayed(this,1000)

        }
    }
    override fun initView() {
        binding.click.visibility = View.VISIBLE
        binding.back.setOnClickListener { finish() }
        binding.tv.text = "考勤打卡"

        binding.layoutCurrentLocation.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_CLOCK).navigation()
        }
        //下班打卡
        binding.layoutAddAttendancesAfternoon.setOnClickListener {
            val millis2 = System.currentTimeMillis()
            var time2 = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            afternoonCheckIn = time2.format(millis2)
            SPUtils.getInstance().put("after",afternoonCheckIn) // 保存下班打卡时间
            ToastUtils.showLong("下班打卡")
            val id = SPUtils.getInstance().getInt("id")
            val morningTime = SPUtils.getInstance().getString("time")
            val status = SPUtils.getInstance().getString("status")

            vm.addKaoqin(
                KaoqinData(
                    afternoonCheckIn,"lat,lon",
                    id  ,"天津八维",morningTime,
                    "2",1
                ))
        }
        //点击 打卡
        binding.btnAddAttendances.setOnClickListener {
            val millis1 = System.currentTimeMillis()
            var time1 = SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
            moring = time1.format(millis1)
            //保存上班打卡时间
            vm.addKaoqin(
                KaoqinData(
                afternoonCheckIn,"lat,lon",
                0 ,"天津八维",moring,
                "1",1
            ))

        }

        vm.addKaoqin.observe(this){
            ToastUtils.showLong("打卡成功")
            SPUtils.getInstance().put("id",it.id)
            SPUtils.getInstance().put("time",it.morningCheckIn)
            SPUtils.getInstance().put("status",it.status)
        }

    }
//    // 计算时间
//    @SuppressLint("SimpleDateFormat")
//    fun checkoutTime(kaoqinData: KaoqinData){
//        val format1 = SimpleDateFormat("HH:mm:ss", Locale.getDefault())
//        val morning = format1.parse(kaoqinData.morningCheckIn)
//        val after = format1.parse(kaoqinData.afternoonCheckIn)
//
//        val morningCalendar = Calendar.getInstance()
//        morningCalendar.time = morning
//
//        val nineOClock = Calendar.getInstance()
//        nineOClock.set(Calendar.HOUR_OF_DAY, 9)
//        nineOClock.set(Calendar.MINUTE, 0)
//        nineOClock.set(Calendar.SECOND, 0)
//        nineOClock.set(Calendar.MILLISECOND, 0)
//        val afterClock = Calendar.getInstance()
//        afterClock.set(0,0,0,18,0)
//      // 1 正常 // 3 迟到  2 早退  4 缺勤
//        if (morning.after(nineOClock.time)) {
//            kaoqinData.status = "3" // 上班打卡时间在 9点 之后 迟到
//        } else if (after.before(afterClock.time)){
//            kaoqinData.status = "2" // 下班打卡时间 在18点之前 早退
//        } else if (morning.time.equals(nineOClock.time) && after.time.equals(afterClock.time)){
//            kaoqinData.status = "1"
//        } else if (morning.time.equals("")){
//            kaoqinData.status = "4"
//        }
//    }
    override fun onResume() {
        super.onResume()
        handeler.post(Uptime)
    }

    override fun onDestroy() {
        super.onDestroy()
        handeler.removeCallbacks(Uptime)
    }
    override fun initData() {

    }

}